package com.pv.bfc.utils;

import com.pv.bfc.model.BriefcaseUser;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class SendPasswordImpl implements SendPassword {

	private MailSender mailSender;
	private SimpleMailMessage message;
	
	private Log log = LogFactory.getLog(SendPasswordImpl.class);

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void setMessage(SimpleMailMessage message) {
		this.message = message;
	}

	@Override
	public void sendEmail(BriefcaseUser user) {
		log.info("IN - SendPasswordImpl - sendEmail");
		// Create a thread safe "sandbox" of the message
		SimpleMailMessage msg = new SimpleMailMessage(this.message);
		msg.setTo(user.getEmail());
		msg.setText("Dear "
				+ user.getFirstName()
				+ " " + user.getLastName()
				+ ", "
				+ "\n\n Below are your User name and Password"
				+ "\n\n Username - "+ user.getUserName()
				+ "\n\n Password - "+ user.getConfirmPassword()
				+"\n\n"
				+"\n\n"
				+"Thanks");
		try {
			mailSender.send(msg);
			log.info("OUT - SendPasswordImpl - sendEmail");
		} catch (MailException ex) {
			// log it and go on
			System.err.println(ex.getMessage());
		}
	}
}
